package com.ytf.test.tenth;

/**
 * @author: YTF
 * @date: 2025/3/29
 * @version: 1.0
 * @description: 闭包和回调
 */
interface IncrementAble{
    void increment();
}
class Callee1 implements IncrementAble{
    private int i=0;
    @Override
    public void increment() {
        i++;
        System.out.println(i);
    }
}
class MyIncrement{
    public void increment(){
        System.out.println("Other operation");
    }
    static void f(MyIncrement mi){
        mi.increment();
    }
}
class Callee2 extends MyIncrement{
    private int i=0;
    @Override
    public void increment() {
        super.increment();
        i++;
        System.out.println(i);
    }
    private class CloSure implements IncrementAble{
        @Override
        public void increment() {
            Callee2.this.increment();
        }
    }
    IncrementAble getIncrementAble(){
        return new CloSure();
    }
}
class Caller{
    private IncrementAble incrementAble;

    public Caller(IncrementAble incrementAble) {
        this.incrementAble = incrementAble;
    }
    void go(){
        incrementAble.increment();
    }
}
public class CallBacks {
    public static void main(String[] args) {
        Callee1 c1 = new Callee1();
        Callee2 c2 = new Callee2();
        MyIncrement.f(c2);
        Caller caller1 = new Caller(c1);
        Caller caller2 = new Caller(c2.getIncrementAble());
        caller1.go();
        caller1.go();
        caller2.go();
        caller2.go();
        /**
         * Outpyt:
         * Other operation
         * 1
         * 1
         * 2
         * Other operation
         * 2
         * Other operation
         * 3
         */
    }
}
